package Yunzai

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"os"
	"time"
	"github.com/olekukonko/tablewriter"
	"strconv"

)
// ToDo: 读取配置文件，根据配置文件做格式化显示

func Show_Info(GoBoxPath,YunZaiBotPath string) {
	os.Chdir(GoBoxPath)
	configPath1 := YunZaiBotPath + "/config/config/qq.yaml"
	configPath2 := YunZaiBotPath + "/config/config/other.yaml"

	// 判断配置文件是否存在
	if _, err := os.Stat(configPath1); os.IsNotExist(err) {
		fmt.Println(Red, "配置文件不存在，请完成初始化...", Reset)
		time.Sleep(2 * time.Second)
		return
	}
	if _, err := os.Stat(configPath2); os.IsNotExist(err) {
		fmt.Println(Red, "配置文件不存在，请完成初始化...", Reset)
		time.Sleep(2 * time.Second)
		return
	}
	var accountConfig Account
	var adminConfig Admin
	// 读取配置文件
	qqdata, err := readFile(configPath1)
	if err != nil {
		fmt.Println(Red, "读取配置文件失败：", err, Reset)
		time.Sleep(2 * time.Second)
		return
	}
	otherdata, err := readFile(configPath2)
	if err != nil {
		fmt.Println(Red, "读取配置文件失败：", err, Reset)
		time.Sleep(2 * time.Second)
		return
	}

	// 解析配置文件
	err = yaml.Unmarshal(qqdata, &accountConfig)
	if err != nil {
		fmt.Println(Red, "解析配置文件失败", err, Reset)
		time.Sleep(2 * time.Second)
		return
	}
	err = yaml.Unmarshal(otherdata, &adminConfig)
	if err != nil {
		fmt.Println(Red, "解析配置文件失败", err, Reset)
		time.Sleep(2 * time.Second)
		return
	}
	table1 := tablewriter.NewWriter(os.Stdout)
	table1.SetHeader([]string{"QQ", "密码", "登录方式"})
	//获取配置文件的内容
	qq := accountConfig.QQ
	password := accountConfig.Password
	Login_type := accountConfig.Login_type
	// 转换数字类型
	numlogin_type, err := strconv.Atoi(Login_type)
	if err != nil {
		// 处理错误
		fmt.Println(Red, "登录接口错误...", Reset)
	}

	device := "未知设备"
	if numlogin_type == 1 {
		device = "安卓手机"
	} else if numlogin_type == 2 {
		device = "aPad"
	} else if numlogin_type == 3 {
		device = "安卓手表"
	} else if numlogin_type == 4 {
		device = "MacOS"
	} else if numlogin_type == 5 {
		device = "iPad"
	} else if numlogin_type == 6 {
		device = "Tim"
	}
	// 表格
	table1.Append([]string{qq, password, device})
	table1.SetColMinWidth(0, 12)
	table1.SetColMinWidth(1, 26)
	table1.SetColMinWidth(2, 12)
	// 自动换行
	table1.SetAutoWrapText(false)
	//居中
	table1.SetAlignment(tablewriter.ALIGN_CENTER)
	fmt.Println("账户信息:")
	table1.Render()

	table2 := tablewriter.NewWriter(os.Stdout)
	table2.SetHeader([]string{"ID", "主人 QQ"})
	for i, qq := range adminConfig.MasterQQ {
		table2.Append([]string{strconv.Itoa(i + 1), strconv.FormatInt(qq, 10)})
	}
	//表格靠右边一点点


	table2.SetColMinWidth(0, 12)
	table2.SetColMinWidth(1, 41)
	//居中
	table2.SetAlignment(tablewriter.ALIGN_CENTER)
	fmt.Println("主人 QQ:")
	table2.Render()
	fmt.Printf("按回车键继续...")
	fmt.Scanln()
}

// ToDo: 读取管理员列表，根据管理员列表做格式化显示
